﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main.ManufactureManage
{
    public partial class FrmSelectPrinter : UniRlv.UI.Main.DialogBase
    {
        List<LineDevice> printers;

        public FrmSelectPrinter()
        {
            InitializeComponent();
        }

        private void FrmSelectPrinter_Load(object sender, EventArgs e)
        {
            Wait();
            string msg = string.Empty;

            printers = DALOpr.Query<LineDevice>("IO=" + (int)input_output.OUTPUT + " and deviceType=" + (int)out_device_type.ODT_PRINTER, ref msg);

            if (!string.IsNullOrEmpty(msg))
            {
                ShowErrorMessageBox("出现错误", msg);
            }
            else
            {
                if (printers == null || printers.Count == 0)
                {
                    ShowErrorMessageBox("错误", "缺少打印机信息");
                }
                else
                {
                    UIHelper.InitDataGridView(dgvPrinters);
                    dgvPrinters.DataSource = printers;

                    if (this.Tag != null)
                    {
                        PrintInfo pi = this.Tag as PrintInfo;
                        txtCodePath.Text = pi.CodePath;
                        txtTemplatePath.Text = pi.TemplatePath;
                        numPrintQty.Value = pi.PrintQty;
                        txtInfoName.Text = pi.Name;
                        for (int i = 0; i < printers.Count; i++)
                        {
                            if (pi.InitString == printers[i].initString && pi.StationNum == printers[i].stationNumber)
                            {
                                dgvPrinters.SetRowSelect(i);
                            }
                        }
                    }
                }

            }

            WaitBack();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (dgvPrinters.CurrentRow == null)
            {
                ShowWarnMessageBox("提示", "先选择打印机");
                return;
            }

            if (string.IsNullOrEmpty(txtCodePath.Text))
            {
                ShowWarnMessageBox("提示", "先选择条码数据源");
                return;
            }

            if (string.IsNullOrEmpty(txtTemplatePath.Text))
            {
                ShowWarnMessageBox("提示", "先选择打印模板");
                return;
            }

            if (string.IsNullOrEmpty(txtInfoName.Text))
            {
                ShowWarnMessageBox("提示", "信息描述不能为空");
                return;
            }

            LineDevice d = dgvPrinters.CurrentRow.DataBoundItem as LineDevice;

            PrintInfo p;

            if (this.Tag != null)
            {
                p = this.Tag as PrintInfo;
            }
            else
            {
                p = new PrintInfo();
                p.ID = DALOpr.GetNextObjectID("PrintInfo");
                p.SrcID = p.ID;
            }

            p.InitString = d.initString;
            p.Name = txtInfoName.Text;
            p.StationNum = d.stationNumber;
            p.SubDeviceNum = d.subDeviceNumber;

            p.PrintQty = Convert.ToInt32(numPrintQty.Value);
            p.TemplatePath = txtTemplatePath.Text;
            p.CodePath = txtCodePath.Text;
            

            this.Tag = p;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnCodeSrcBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.txt)文本文件|*.txt|(*.*)所有文件|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCodePath.Text = ofd.FileName;
            }
        }

        private void btnTemplateBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "(*.btw)文本文件|*.btw|(*.*)所有文件|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtTemplatePath.Text = ofd.FileName;
            }
        }
    }
}
